package com.yunwaikeji.gy_tool_library.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import com.yunwaikeji.gy_tool_library.R;
import com.yunwaikeji.gy_tool_library.utils.display.GYDisplayUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 柱状条形图
 * 郭杨
 * 2020/10/20
 */

public class GYColumnarBarView extends View {
    //渐变色顶部颜色
    private int topColor;
    //渐变色底部颜色
    private int bottomColor;
    //View重绘延时时间
    private long delayTime;
    //每个小矩形的宽度
    private float rectWidth;
    //每个小矩形的高度
    private float rectHeight;
    //绘制小矩形的画笔
    private Paint paint;
    //View的宽度
    private int viewWidth;
    //View的高度
    private int viewHeight;
    //产生渐变效果
    private LinearGradient linearGradient;
    private List<Integer> rectHeightList = new ArrayList<>();
    //每个小矩形之间的距离
    private float rectSpacing;
    //高度基准值
    private int maxNumber = 100;

    /**
     * 代码中直接new时调用
     *
     * @param context
     */
    public GYColumnarBarView(Context context) {
        this(context, null);
    }

    /**
     * 在xml中使用自定义View并且没有自定义属性时调用
     *
     * @param context
     * @param attrs
     */
    public GYColumnarBarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    /**
     * 在xml中使用自定义View并且使用自定义属性时调用
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public GYColumnarBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GYColumnarBarView);
        delayTime = typedArray.getInt(R.styleable.GYColumnarBarView_delayTime, 300);
        topColor = typedArray.getColor(R.styleable.GYColumnarBarView_topColor, Color.YELLOW);
        bottomColor = typedArray.getColor(R.styleable.GYColumnarBarView_bottomColor, Color.BLUE);
        rectWidth = typedArray.getDimension(R.styleable.GYColumnarBarView_rectWidth, GYDisplayUtils.dp2px(getContext(),10));
        rectHeight = typedArray.getDimension(R.styleable.GYColumnarBarView_rectHeight, GYDisplayUtils.dp2px(getContext(),50));
        rectSpacing = typedArray.getDimension(R.styleable.GYColumnarBarView_rectSpacing, GYDisplayUtils.dp2px(getContext(),3));
        maxNumber = typedArray.getInteger(R.styleable.GYColumnarBarView_maxNumber, 100);
        String rectHeightString = typedArray.getString(R.styleable.GYColumnarBarView_rectHeightList);
        if (!TextUtils.isEmpty(rectHeightString)){
            String[] strings = rectHeightString.split(",");
            List<String> stringList = Arrays.asList(strings);
            List<Integer> rectHeightList = new ArrayList<>();
            for (String string : stringList){
                rectHeightList.add(Integer.valueOf(string));
            }
            setRectHeights(rectHeightList);
        }else {
            setRectHeights(30,50,40,10,20,40,80,50,30,90);
        }
        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width = 0;
        for (int i = 0;i<rectHeightList.size();i++){
            if (i>0){
                width+=rectSpacing;
            }
            width+=rectWidth;
        }
        int height = Math.round(rectHeight);

        setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : width,
                heightMode == MeasureSpec.EXACTLY ? heightSize : height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0;i<rectHeightList.size();i++){
            int left = 0;
            if (i>0){
                left+=(rectWidth+rectSpacing)*i;
            }
            int top = viewHeight-Math.round(rectHeight)*rectHeightList.get(i)/ maxNumber;
            int right = left+Math.round(rectWidth);
            int bottom = viewHeight;
            canvas.drawRect(left,top,right,bottom,paint);
        }
        //让View延时mDelayTime毫秒再重绘
        postInvalidateDelayed(delayTime);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewWidth = getMeasuredWidth();
        viewHeight = getMeasuredHeight();
//        rectWidth = (int) (viewWidth * 0.6 / rectCount);
        linearGradient = new LinearGradient(0, 0, rectWidth, rectHeight, topColor, bottomColor, Shader.TileMode.CLAMP);
        paint.setShader(linearGradient);
    }

    /**
     * 设置每个柱状条形的高度
     *
     * @param rectHeightList
     */
    public GYColumnarBarView setRectHeights(List<Integer> rectHeightList){
        this.rectHeightList.clear();
        this.rectHeightList.addAll(rectHeightList);
        requestLayout();
        invalidate();
        return this;
    }

    public GYColumnarBarView setRectHeights(Integer... rectHeights){
        return setRectHeights(Arrays.asList(rectHeights));
    }

    public GYColumnarBarView setMaxNumber(int maxNumber){
        this.maxNumber = maxNumber;
        requestLayout();
        invalidate();
        return this;
    }
}
